/*
 * pps_bom
 */
package com.pitop.mould.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MouldMaterialException;
import com.pitop.mould.dao.MouldMaterialDAO;
import com.pitop.mould.entity.MouldMaterial;
import com.pitop.mould.entity.MouldMaterialState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("mouldMaterialSV")
@Slf4j
public class MouldMaterialSVImpl extends BaseSVImpl<MouldMaterial, Long> implements MouldMaterialSV {

    @Autowired
    private MouldMaterialDAO mouldMaterialDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return mouldMaterialDAO;
    }

    /**
     * 保存account对象
     * @param entity 实体
     *
     */
    @Override
    public void save(MouldMaterial entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象MouldMaterial
     *
     * @param id
     * @param materialCode 物料编码
     * @param mouldCode    模具编码
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial load(Long id, String materialCode, String mouldCode) {
        if (id == null && materialCode == null && mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("materialCode", materialCode);
        param.put("mouldCode", mouldCode);
        return mouldMaterialDAO.load(param);
    }

    /**
     * 加载一个对象MouldMaterial,(将查询关联数据)
     *
     * @param id
     * @param materialCode 物料编码
     * @param mouldCode    模具编码
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial get(Long id, String materialCode, String mouldCode) {
        if (id == null && materialCode == null && mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("materialCode", materialCode);
        param.put("mouldCode", mouldCode);
        return mouldMaterialDAO.getDetail(id, materialCode, mouldCode);
    }

    /**
     * 加载一个对象MouldMaterial 通过id
     *
     * @param id
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial loadById(Long id) {
        if (id == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldMaterialDAO.loadById(id);
    }

    /**
     * 加载一个对象MouldMaterial 通过materialCode
     *
     * @param materialCode 物料编码
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial loadByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldMaterialDAO.loadByMaterialCode(materialCode);
    }

    /**
     * 加载一个对象MouldMaterial 通过mouldCode
     *
     * @param mouldCode 模具编码
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial loadByMouldCode(String mouldCode) {
        if (mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldMaterialDAO.loadByMouldCode(mouldCode);
    }

    /**
     * 加载一个对象MouldMaterial 通过id,(将查询关联数据)
     *
     * @param id
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial getById(Long id) {
        if (id == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldMaterialDAO.getById(id);
    }

    /**
     * 加载一个对象MouldMaterial 通过materialCode,(将查询关联数据)
     *
     * @param materialCode 物料编码
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial getByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldMaterialDAO.getByMaterialCode(materialCode);
    }

    /**
     * 加载一个对象MouldMaterial 通过mouldCode,(将查询关联数据)
     *
     * @param mouldCode 模具编码
     * @return MouldMaterial
     */
    @Override
    public MouldMaterial getByMouldCode(String mouldCode) {
        if (mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return mouldMaterialDAO.getByMouldCode(mouldCode);
    }

    /**
     * 根据主键id,oldStates 共同更新 MouldMaterial 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, MouldMaterialState newState, MouldMaterialState... oldStates) {
        if (id == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        mouldMaterialDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键materialCode,oldStates 共同更新 MouldMaterial 的状态到newState状态
     *
     * @param materialCode 物料编码
     * @param newState     新状态
     * @param oldStates    旧状态集合
     */
    @Override
    public void updateStateByMaterialCode(String materialCode, MouldMaterialState newState, MouldMaterialState... oldStates) {
        if (materialCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        mouldMaterialDAO.updateStateByMaterialCode(materialCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键mouldCode,oldStates 共同更新 MouldMaterial 的状态到newState状态
     *
     * @param mouldCode 模具编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByMouldCode(String mouldCode, MouldMaterialState newState, MouldMaterialState... oldStates) {
        if (mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        mouldMaterialDAO.updateStateByMouldCode(mouldCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 MouldMaterial 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, MouldMaterialState state) {
        if (id == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        mouldMaterialDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键materialCode 更新 MouldMaterial 的状态到另一个状态
     *
     * @param materialCode 物料编码
     * @param state        状态
     */
    @Override
    public void updateByMaterialCode(String materialCode, MouldMaterialState state) {
        if (materialCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        mouldMaterialDAO.updateByMaterialCode(materialCode, state.name(), new Date());
    }

    /**
     * 根据主键mouldCode 更新 MouldMaterial 的状态到另一个状态
     *
     * @param mouldCode 模具编码
     * @param state     状态
     */
    @Override
    public void updateByMouldCode(String mouldCode, MouldMaterialState state) {
        if (mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        mouldMaterialDAO.updateByMouldCode(mouldCode, state.name(), new Date());
    }


    /**
     * 删除对象MouldMaterial
     *
     * @param id * @param materialCode 物料编码* @param mouldCode 模具编码
     * @return MouldMaterial
     */
    @Override
    public void delete(Long id, String materialCode, String mouldCode) {
        if (id == null && materialCode == null && mouldCode == null) {
            throw new MouldMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("materialCode", materialCode);
        param.put("mouldCode", mouldCode);
        mouldMaterialDAO.delete(param);
    }


    /**
     * 查询MouldMaterial分页
     *
     * @param mouldMaterial 对象
     * @param offset        查询开始行
     * @param limit         查询行数
     * @return List<MouldMaterial>
     */
    @Override
    public List<MouldMaterial> list(MouldMaterial mouldMaterial, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (mouldMaterial != null) {
            map = JSON.parseObject(JSON.toJSONString(mouldMaterial, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return mouldMaterialDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(MouldMaterial mouldMaterial) {
        Map<String, Object> map = null;
        if (mouldMaterial != null) {
            map = JSON.parseObject(JSON.toJSONString(mouldMaterial, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return mouldMaterialDAO.count(map);
    }

    /**
     * 查询MouldMaterial分页
     *
     * @param id     * @param materialCode  物料编码* @param mouldCode  模具编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MouldMaterial>
     */
    @Override
    public List<MouldMaterial> list(Long id, String materialCode, String mouldCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("materialCode", materialCode);
        param.put("mouldCode", mouldCode);
        return mouldMaterialDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String materialCode, String mouldCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("materialCode", materialCode);
        param.put("mouldCode", mouldCode);
        return mouldMaterialDAO.count(param);
    }

}
